package com.tjetc._4method_quotation;

import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Supplier;

public class _2QuotaObjectMethod {
    public static void main(String[] args) {

        testMemberMethod(new Supplier<>() {
            @Override
            public String get() {
                return "abc".trim();
            }
        });

        // 如果引用的方法无法满足条件 就不能使用
        testMemberMethod(() -> "abc".replaceFirst("", "a"));
        testMemberMethod("abc"::trim);


        testStaticMethod(() -> Math.random());
        testStaticMethod(Math::random);


        testConstructorMethod(name -> {
            return new User(name);
        });
        testConstructorMethod(name -> new User(name));
        testConstructorMethod(User::new);

        testArrayMethod((integer) -> {
            return new int[integer];
        });
        testArrayMethod((integer) -> new int[integer]);
        testArrayMethod(int[]::new);
    }

    // 引用成员方法
    public static void testMemberMethod(Supplier<String> supplier) {
        String i = supplier.get();
        System.out.println(i);
    }
    // 引用静态方法
    public static void testStaticMethod(Supplier<Double> supplier) {
        Double v = supplier.get();
        System.out.println(v);
    }

    // 引用构造方法
    // 给他一个string（zhangsan） 返回一个User对象
    public static void testConstructorMethod(Function<String, User> function) {
        User user = function.apply("zhangsan");
        System.out.println(user.name);
    }


    // 数组引用
    public static void testArrayMethod(Function<Integer, int[]> function) {
        int[] apply = function.apply(1);
        System.out.println(Arrays.toString(apply));
    }
}

class User {
    String name;

    public User(String name) {
        this.name = name;
    }
}


